<template>
  <a-card :bordered="false">
    <div v-if="showActions" class="table-operator" style="display: flex; justify-content: space-between">
      <a-radio-group v-model="queryParam.status" @change="getList">
        <a-radio-button value="-1"> 全部 </a-radio-button>
        <a-radio-button value="0"> 未绑定 </a-radio-button>
        <a-radio-button value="1"> 已绑定 </a-radio-button>
      </a-radio-group>
    </div>
    <a-table
      :rowKey="
        (r, i) => {
          return r.id
        }
      "
      :pagination="{
        total: this.totalCount,
        showSizeChanger: false,
        onChange: this.tablePageChange,
      }"
      :columns="columns"
      :data-source="list"
      :scroll="{ x: 'max-content' }"
    >
      <div slot="user" slot-scope="text, record">
        <div class="name">
          <div class="pic">
            <img :src="record.logo" alt="" />
          </div>
          <div class="info">
            <span>{{ record.name }}</span>
          </div>
        </div>
      </div>

      <span slot="status_bind" slot-scope="text">
        <div class="tagBox">
          <span class="tag" :style="text | orderStatus"></span>
          <span>{{ getStatus(text) }} </span>
        </div>
      </span>

      <div slot="action" slot-scope="scope" v-if="showAction">
        <a-button type="link" @click="$emit('handle', 'edit', scope)" size="small">编辑</a-button>
        <!-- <a-divider type="vertical" style="margin: 0 1px" /> -->

        <!-- <a-dropdown>
          <a-button type="link" size="small">更多<a-icon type="down" /></a-button>
          <a-menu slot="overlay">
            <a-menu-item v-if="authRemove">
              <a-button type="link" size="small" @click="handel('remove', scope)">删除</a-button>
            </a-menu-item>
            <template v-if="authChangeStatus && parseInt(scope.status) === 0">
              <a-menu-item>
                <a-button type="link" size="small" @click="handel('change', scope, 1)">同意</a-button>
              </a-menu-item>
              <a-menu-item>
                <a-button type="link" size="small" @click="handel('change', scope, 2)">拒绝</a-button>
              </a-menu-item>
            </template>
          </a-menu>
        </a-dropdown> -->
      </div>
    </a-table>
  </a-card>
</template>

<script>
import { bindStoreList, bindStatus, bindDel } from '@/api/warehouse.js'
const columns = [
  {
    title: 'ID',
    dataIndex: 'id',
    width: 80,
  },
  {
    title: '商家信息',
    scopedSlots: {
      customRender: 'user',
    },
    width: 280,
  },
  {
    title: '联系电话',
    dataIndex: 'contact_tel',
    scopedSlots: {
      customRender: 'contact_tel',
    },
  },
  {
    title: '关联仓库',
    dataIndex: 'front_agent.name',
  },
  {
    title: '状态',
    dataIndex: 'status_bind',
    scopedSlots: {
      customRender: 'status_bind',
    },
  },
  {
    title: '操作',
    width: 180,
    scopedSlots: {
      customRender: 'action',
    },
  },
]

export default {
  props: {
    params: {
      type: Object,
      default() {
        return {}
      },
    },

    showAction: {
      type: Boolean,
      default: true,
    },

    authAdd: {
      type: Boolean,
      default: true,
    },

    authEdit: {
      type: Boolean,
      default: true,
    },

    authRemove: {
      type: Boolean,
      default: true,
    },

    authChangeStatus: {
      type: Boolean,
      default: true,
    },

    showActions: {
      type: Boolean,
      default: true,
    },
  },

  data() {
    return {
      totalCount: 0,
      columns,
      list: [],

      queryParam: {
        status: '-1',
        page: 1,
        limit: 10,
      },
    }
  },

  computed: {
    getStatus() {
      return (status) => {
        let str

        switch (parseInt(status)) {
          case 0:
            str = '未绑定'
            break

          case 1:
            str = '已绑定'
            break
        }
        return str
      }
    },
    userInfo() {
      return this.$store.state.user
    },
  },

  filters: {
    orderStatus(val) {
      if (val == 0) {
        return 'background-color:#ff0000;'
      }

      if (val == 1) {
        return 'background-color:#65c817;'
      }

      // if (val == 2) {
      //   return 'background-color:#ff0000;'
      // }
    },
  },

  methods: {
    // 分页请求
    tablePageChange(e) {
      this.queryParam.page = e
      this.getList()
    },

    // 获取数据
    async getList() {
      const hide = this.$message.loading('加载中...', 1000)
      let { queryParam, params } = this
      const res = await bindStoreList({
        ...queryParam,
        ...params,
      })
      if (res.code == 0) {
        this.list = res.data.data
        this.totalCount = res.data.totalCount
      }
      hide()
    },

    callback(res) {
      if (!res.code) {
        this.getList()
        this.$message.success(res.msg)
        return
      }

      this.$message.error(res.msg)
    },

    bindDel(ids) {
      this.$confirm({
        title: '是否删除供货商？',
        onOk: async () => {
          const res = await bindDel({ ids })
          this.callback(res)
        },
        onCancel() {},
      })
    },

    bindStatus(ids, status) {
      this.$confirm({
        title: '是否确认当前操作？',
        onOk: async () => {
          const res = await bindStatus({ ids, status })
          this.callback(res)
        },
        onCancel() {},
      })
    },

    handel(type, item, status) {
      switch (type) {
        case 'remove':
          this.bindDel(item.id)
          break

        case 'change':
          this.bindStatus(item.id, status)
          break
      }
    },
  },
}
</script>

<style lang="less" scoped>
.tagBox {
  display: flex;
  align-items: center;
}

.tag {
  display: inline-block;
  width: 6px;
  height: 6px;
  border-radius: 50%;
  margin-right: 4px;
}

.name {
  display: flex;
  align-items: center;
  .pic {
    width: 48px;
    min-width: 48px;
    height: 48px;
    border-radius: 4px;
    overflow: hidden;
    margin-right: 24px;
    img {
      width: 100%;
      height: 100%;
    }
  }
  .info {
    flex: auto;
    width: 0;
    display: flex;
    flex-direction: column;
    font-size: 14px;
    span {
      width: 100%;
      overflow: hidden;
      white-space: nowrap;
      text-overflow: ellipsis;
    }
    span:nth-child(2) {
      color: rgba(0, 0, 0, 0.45);
      margin-top: 6px;
    }
  }
}
</style>
